﻿' -----------------------------------------------------------------------------
Imports System
Imports System.Runtime.InteropServices    '  SizeOf
Imports System.Text                       '  StringBuilder


'定义注册表函数

Public Class WinCeReg

    Public Enum HKEY
        HKEY_CLASSES_ROOT = &H80000000
        HKEY_CURRENT_USER = &H80000001
        HKEY_LOCAL_MACHINE = &H80000002
        HKEY_USERS = &H80000003
        HKEY_PERFORMANCE_DATA = &H80000004
        HKEY_PERFORMANCE_TEXT = &H80000050
        HKEY_PERFORMANCE_NLSTEXT = &H80000060
        HKEY_CURRENT_CONFIG = &H80000005
    End Enum

    Public Enum REGDISP
        REG_CREATED_NEW_KEY = &H1
        REG_OPENED_EXISTING_KEY = &H2
    End Enum

    Public Enum REGTYPE
        REG_NONE = 0
        REG_SZ = 1
        REG_EXPAND_SZ = 2
        REG_BINARY = 3
        REG_DWORD = 4
        REG_DWORD_LITTLE_ENDIAN = 4
        REG_DWORD_BIG_ENDIAN = 5
        REG_LINK = 6
        REG_MULTI_SZ = 7
        REG_RESOURCE_LIST = 8
    End Enum

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegCloseKey( _
         ByVal hKey As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegOverridePredefKey( _
         ByVal hKey As IntPtr _
       , ByVal hNewHKey As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegOpenUserClassesRoot( _
       ByVal hToken As IntPtr _
       , ByVal dwOptions As Integer _
       , ByVal samDesired As Integer _
       , ByVal phkResult As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegOpenCurrentUser( _
       ByVal samDesired As Integer _
       , ByVal phkResult As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegDisablePredefinedCache( _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegConnectRegistry( _
       ByVal lpMachineName As String _
       , ByVal hKey As IntPtr _
       , ByVal phkResult As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegCreateKey( _
         ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       , ByVal phkResult As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegCreateKeyEx( _
         ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       , ByVal Reserved As Integer _
       , ByVal lpClass As String _
       , ByVal dwOptions As Integer _
       , ByVal samDesired As Integer _
       , ByVal lpSecurityAttributes As IntPtr _
       , ByRef phkResult As IntPtr _
       , ByRef lpdwDisposition As WinCeReg.REGDISP _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegDeleteKey( _
         ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegDeleteValue( _
         ByVal hKey As IntPtr _
       , ByVal lpValueName As String _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegEnumKey( _
         ByVal hKey As IntPtr _
       , ByVal dwIndex As Integer _
       , ByVal lpName As String _
       , ByVal cbName As Integer _
       ) As Integer
    End Function

    <DllImport("CoreDll.DLL")> _
    Public Shared Function RegEnumKeyEx( _
         ByVal hKey As IntPtr _
       , ByVal dwIndex As Integer _
       , ByVal lpName As String _
       , ByRef lpcbName As Integer _
       , ByVal lpReserved As Integer _
       , ByVal lpClass As String _
       , ByRef lpcbClass As Integer _
       , ByVal lpftLastWriteTime As Integer _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegEnumValue( _
       ByVal hKey As IntPtr _
       , ByVal dwIndex As Integer _
       , ByVal lpValueName As String _
       , ByRef lpcbValueName As Integer _
       , ByRef lpReserved As Integer _
       , ByRef lpType As Integer _
       , ByRef lpData As Byte _
       , ByRef lpcbData As Integer _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegLoadKey( _
       ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       , ByVal lpFile As String _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegNotifyChangeKeyValue( _
         ByVal hKey As IntPtr _
       , ByVal bWatchSubtree As Integer _
       , ByVal dwNotifyFilter As Integer _
       , ByVal hEvent As IntPtr _
       , ByVal fAsynchronus As Integer _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegOpenKey( _
         ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       , ByVal phkResult As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDll.DLL")> _
    Public Shared Function RegOpenKeyEx( _
         ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       , ByVal ulOptions As Integer _
       , ByVal samDesired As Integer _
       , ByRef phkResult As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegQueryValue( _
       ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       , ByVal lpValue As String _
       , ByRef lpcbValue As Integer _
       ) As Integer
    End Function

    '  System.IntPtr version
    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegQueryValueEx( _
         ByVal hKey As IntPtr _
       , ByVal lpValueName As String _
       , ByVal Res1 As Integer _
       , ByRef lpType As REGTYPE _
       , ByVal iptrData As IntPtr _
       , ByRef lpcbData As Integer _
       ) As Integer
    End Function

    '  System.StringBuilder version
    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegQueryValueEx( _
         ByVal hKey As IntPtr _
       , ByVal lpValueName As String _
       , ByVal Res1 As Integer _
       , ByRef lpType As REGTYPE _
       , ByVal strData As StringBuilder _
       , ByRef lpcbData As Integer _
       ) As Integer
    End Function

    '  System.Integer version
    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegQueryValueEx( _
         ByVal hKey As IntPtr _
       , ByVal lpValueName As String _
       , ByVal Res1 As Integer _
       , ByRef lpType As REGTYPE _
       , ByRef piData As Integer _
       , ByRef lpcbData As Integer _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegReplaceKey( _
       ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       , ByVal lpNewFile As String _
       , ByVal lpOldFile As String _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegRestoreKey( _
       ByVal hKey As IntPtr _
       , ByVal lpFile As String _
       , ByVal dwFlags As Integer _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegSaveKey( _
       ByVal hKey As IntPtr _
       , ByVal lpFile As String _
       , ByVal lpSecurityAttributes As IntPtr _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegSetValue( _
         ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       , ByVal dwType As Integer _
       , ByVal lpData As String _
       , ByVal cbData As Integer _
       ) As Integer
    End Function

    '  System.IntPtr version
    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegSetValueEx( _
         ByVal hKey As IntPtr _
       , ByVal lpValueName As String _
       , ByVal Res1 As Integer _
       , ByVal dwType As REGTYPE _
       , ByRef lpData As IntPtr _
       , ByVal cbData As Integer _
       ) As Integer
    End Function

    '  System.Integer version
    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegSetValueEx( _
       ByVal hKey As IntPtr _
       , ByVal lpValueName As String _
       , ByVal Res1 As Integer _
       , ByVal dwType As REGTYPE _
       , ByRef piData As Integer _
       , ByVal cbData As Integer _
       ) As Integer
    End Function

    '  System.String version
    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegSetValueEx( _
       ByVal hKey As IntPtr _
       , ByVal lpValueName As String _
       , ByVal Res1 As Integer _
       , ByVal dwType As REGTYPE _
       , ByVal strData As String _
       , ByVal cbData As Integer _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegUnLoadKey( _
       ByVal hKey As IntPtr _
       , ByVal lpSubKey As String _
       ) As Integer
    End Function

    <DllImport("CoreDLL.DLL")> _
    Public Shared Function RegSaveKeyEx( _
       ByVal hKey As IntPtr _
       , ByVal lpFile As String _
       , ByVal lpSecurityAttributes As IntPtr _
       , ByVal Flags As Integer _
       ) As Integer
    End Function

End Class

'注册表读写函数
Public Class lvlRegistry
    Private hkeyHive As New IntPtr(WinCeReg.HKEY.HKEY_LOCAL_MACHINE)
    Private strYDKey As String = "SOFTWARE\GCS"
    Private iType As WinCeReg.REGTYPE
    Private hkeyCurrent As IntPtr
    Private intReturn, cbData As Integer
    Private regdispResult As WinCeReg.REGDISP
    Private Const cbMAX As Integer = 255

    '  Delete a registry key.
    Public Function DeleteKey(ByVal strKeyName As String _
                             ) _
                             As Boolean

        intReturn = WinCeReg.RegDeleteKey( _
                     hkeyHive, _
                     strYDKey + "\" + strKeyName _
                     )
        If intReturn <> 0 Then Return False

        Return True
    End Function

    '  Read a System.String from the registry.
    Public Function GetValue(ByVal strKeyName As String, _
                             ByVal strValueName As String, _
                             ByRef strValue As String _
                             ) _
                             As Boolean

        '  Open HKEY_LOCAL_MACHINE\SOFTWARE\YaoDurant\... 
        intReturn = WinCeReg.RegCreateKeyEx( _
                       hkeyHive, strYDKey + "\" + strKeyName, _
                       0, String.Empty, 0, 0, IntPtr.Zero, _
                       hkeyCurrent, _
                       regdispResult)
        If intReturn <> 0 Then Return False

        '  Create fields to hold the output.
        Dim sbValue As New StringBuilder(cbMAX)
        Dim cbValue As Integer = cbMAX

        '  Pead the value from the registry into sbValue
        intReturn = WinCeReg.RegQueryValueEx( _
                       hkeyCurrent, strValueName, _
                       0, 0, _
                       sbValue, cbValue)
        If intReturn <> 0 Then Return False

        '  Close the key.
        intReturn = WinCeReg.RegCloseKey(hkeyCurrent)
        If intReturn <> 0 Then Return False

        '  Set the string into the output parameter.
        strValue = sbValue.ToString()
        If strValue Is Nothing Then strValue = String.Empty


        Return True
    End Function

    '  Read a System.Integer from the registry.
    Public Function GetValue(ByVal strKeyName As String, _
                             ByVal strValueName As String, _
                             ByRef intValue As Integer _
                             ) _
                             As Boolean

        '  Open HKEY_LOCAL_MACHINE\SOFTWARE\YaoDurant\... 
        intReturn = WinCeReg.RegCreateKeyEx( _
                       hkeyHive, strYDKey + "\" + strKeyName, _
                       0, String.Empty, 0, 0, IntPtr.Zero, _
                       hkeyCurrent, _
                       regdispResult)
        If intReturn <> 0 Then Return False

        '  Pull the value into intValue.  For platform 
        '     independence, use Marshal.SizeOf(intValue),
        '     not "4", to specify the size in bytes of
        '     a System.Integer.
        Dim cbValue As Integer = Marshal.SizeOf(intValue)
        intReturn = WinCeReg.RegQueryValueEx( _
                       hkeyCurrent, strValueName, _
                       0, 0, _
                       intValue, cbValue)
        If intReturn <> 0 Then Return False

        '  Close the key.
        intReturn = WinCeReg.RegCloseKey(hkeyCurrent)
        If intReturn <> 0 Then Return False

        Return True
    End Function

    '  Read a System.Boolean from the registry.
    Public Function GetValue(ByVal strKeyName As String, _
                             ByVal strValueName As String, _
                             ByRef boolValue As Boolean _
                             ) _
                             As Boolean

        '  Use the integer version of GetValue to get the value 
        '     from the registry, then convert it to a boolean.
        Dim intValue As Integer
        Dim boolReturn As Boolean = _
           GetValue(strKeyName, strValueName, intValue)
        If boolReturn Then _
           boolValue = Convert.ToBoolean(intValue)
        Return boolReturn
    End Function

    '  Write a System.String to the registry.
    Public Function SetValue(ByVal strKeyName As String, _
                             ByVal strValueName As String, _
                             ByVal strValue As String _
                             ) _
                             As Boolean

        '  Open HKEY_LOCAL_MACHINE\SOFTWARE\YaoDurant\... 
        intReturn = WinCeReg.RegCreateKeyEx( _
                       hkeyHive, strYDKey + "\" + strKeyName, _
                       0, String.Empty, 0, 0, IntPtr.Zero, _
                       hkeyCurrent, _
                       regdispResult)
        If intReturn <> 0 Then Return False

        '  Store strValue under the name strValueName.
        intReturn = WinCeReg.RegSetValueEx( _
                       hkeyCurrent, strValueName, _
                       0, WinCeReg.REGTYPE.REG_SZ, _
                       strValue, strValue.Length * 2 + 1)
        If intReturn <> 0 Then Return False

        '  Close the key.
        intReturn = WinCeReg.RegCloseKey(hkeyCurrent)
        If intReturn <> 0 Then Return False

        Return True
    End Function

    '  Write a System.Integer to the registry.
    Public Function SetValue(ByVal strKeyName As String, _
                             ByVal strValueName As String, _
                             ByVal intValue As Integer _
                             ) _
                             As Boolean

        '  Open HKEY_LOCAL_MACHINE\SOFTWARE\YaoDurant\... 
        intReturn = WinCeReg.RegCreateKeyEx( _
                       hkeyHive, strYDKey + "\" + strKeyName, _
                       0, String.Empty, 0, 0, IntPtr.Zero, _
                       hkeyCurrent, _
                       regdispResult)
        If intReturn <> 0 Then Return False

        '  Store intValue under the name strValueName.  For
        '  platform independence, use Marshal.SizeOf(intValue),
        '     not "4", to specify the size in bytes of
        '     a System.Integer.
        intReturn = WinCeReg.RegSetValueEx( _
                       hkeyCurrent, strValueName, _
                       0, 0, _
                       intValue, Marshal.SizeOf(intValue))
        If intReturn <> 0 Then Return False

        '  Close the key.
        intReturn = WinCeReg.RegCloseKey(hkeyCurrent)
        If intReturn <> 0 Then Return False

        Return True
    End Function

    '  Write a System.Boolean to the registry.
    Public Function SetValue(ByVal strKeyName As String, _
                             ByVal strValueName As String, _
                             ByVal boolValue As Boolean _
                             ) _
                             As Boolean

        '  Cast the value as a boolean, then use the integer 
        '     version of SetValue to set the value into the
        '     registry.

        '  There is no Convert.ToInteger method.  For platform
        '     independence, we cast to the smallest integer, 
        '     which will always implicitly and successfully cast
        '     to Integer.
        Return SetValue(strKeyName, _
                        strValueName, _
                        Convert.ToInt16(boolValue))

    End Function

    Public Enum DistReadStyle As Integer
        测上下丝 = 0
        直接读视距 = 1
    End Enum

    Public Structure OptionDefine
        Public Project As String                        '工程名称
        Public ComparedFile As String                   '比对文件
        Public SaveTo As String                         '保存文件

        Public BMs As String                            '基准点
        Public Read_Unit As String                      '中丝读数单位
        Public Read_JiFuDistance As String              '基辅常数
        Public Dist_ReadStyle As String                 '视距测量类型
        Public Dist_UnitofUpDownRead As String          '上下丝读数单位
        Public Dist_MultofUpDownRead As String          '视距乘常数
        Public Dist_UnitofRead As String                '视距读数单位

        Public XC_TwiceRead As String                   '两次读数差
        Public XC_JiFuHeightErr As String               '基辅高差之差
        Public XC_MinRead As String                     '最小读数
        Public XC_Distace As String                     '最大视距
        Public XC_BackForeDistanceGap As String         '前后视距差
        Public XC_BackForeDistanceGapTotal As String    '前后视距累计差

    End Structure

    'Public Shared OptionDef As OptionDefine
    'Public Shared ProjectAndFileSetted As Boolean = False  '控制设置对话框是否读入工程名称等

    Sub New()
        'With OptionDef
        '    If .Read_Unit = String.Empty Then
        '        .Read_Unit = "0.00001"
        '    End If
        '    If .Read_JiFuDistance = String.Empty Then
        '        .Read_JiFuDistance = "3.0155"
        '    End If
        'End With
    End Sub


    Public Sub RecordOptions()
        'Dim prj As String = OptionDef.Project
        'SetValue("沉降测量", "当前工程", OptionDef.Project)

        'strYDKey &= "\沉降测量"
        'SetValue(prj, "比对文件", OptionDef.ComparedFile)
        'SetValue(prj, "保存文件", OptionDef.SaveTo)
        'SetValue(prj, "基准点", OptionDef.BMs)
        'SetValue(prj, "中丝单位", OptionDef.Read_Unit)
        'SetValue(prj, "基辅常数", OptionDef.Read_JiFuDistance)
        'SetValue(prj, "视距测量方法", OptionDef.Dist_ReadStyle)
        'If OptionDef.Dist_ReadStyle = "0" Then
        '    SetValue(prj, "上下丝读数单位", OptionDef.Dist_UnitofUpDownRead)
        '    SetValue(prj, "视距乘常数", OptionDef.Dist_MultofUpDownRead)
        'ElseIf OptionDef.Dist_ReadStyle = "1" Then
        '    SetValue(prj, "视距读数单位", OptionDef.Dist_UnitofRead)
        'End If
        'SetValue(prj, "两次读数差", OptionDef.XC_TwiceRead)
        'SetValue(prj, "基辅高差之差", OptionDef.XC_JiFuHeightErr)
        'SetValue(prj, "最小读数", OptionDef.XC_MinRead)
        'SetValue(prj, "最大视距", OptionDef.XC_Distace)
        'SetValue(prj, "前后视距差", OptionDef.XC_BackForeDistanceGap)
        'SetValue(prj, "前后视距累计差", OptionDef.XC_BackForeDistanceGapTotal)
    End Sub

    Public Sub ReadOptions()

        'GetValue("沉降测量", "当前工程", OptionDef.Project)
        'Dim prj As String = OptionDef.Project

        'strYDKey &= "\沉降测量"
        'GetValue(prj, "比对文件", OptionDef.ComparedFile)
        'GetValue(prj, "保存文件", OptionDef.SaveTo)
        'GetValue(prj, "基准点", OptionDef.BMs)
        'GetValue(prj, "中丝单位", OptionDef.Read_Unit)
        'GetValue(prj, "基辅常数", OptionDef.Read_JiFuDistance)
        'GetValue(prj, "视距测量方法", OptionDef.Dist_ReadStyle)
        'GetValue(prj, "上下丝读数单位", OptionDef.Dist_UnitofUpDownRead)
        'GetValue(prj, "视距乘常数", OptionDef.Dist_MultofUpDownRead)
        'GetValue(prj, "视距读数单位", OptionDef.Dist_UnitofRead)
        'GetValue(prj, "两次读数差", OptionDef.XC_TwiceRead)
        'GetValue(prj, "基辅高差之差", OptionDef.XC_JiFuHeightErr)
        'GetValue(prj, "最小读数", OptionDef.XC_MinRead)
        'GetValue(prj, "最大视距", OptionDef.XC_Distace)
        'GetValue(prj, "前后视距差", OptionDef.XC_BackForeDistanceGap)
        'GetValue(prj, "前后视距累计差", OptionDef.XC_BackForeDistanceGapTotal)
    End Sub

    Public Sub CurrentProject(ByVal prj As String)
        SetValue("沉降测量", "当前工程", prj)
    End Sub

End Class


